package com.southwind.job;


import com.southwind.entity.RedisConstants;
import com.southwind.util.AliOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.CollectionUtils;

import java.util.Set;

@Configuration
public class DeleteImageJob {
    @Autowired
    private RedisTemplate redisTemplate;


    @Scheduled(cron = "0 0 0 1 * ?")
//    @Scheduled(cron = "*/30 * * * * ?")
    public void deleteImage() {
        System.out.println("DeleteImageJob.deleteImage");
        Set members = redisTemplate.opsForSet().members(RedisConstants.USE_IMAGE);
        Set<String> set = redisTemplate.opsForSet().difference(RedisConstants.UPLOAD_IMAGE, RedisConstants.USE_IMAGE);
        if (!CollectionUtils.isEmpty(members)&&!CollectionUtils.isEmpty(set)) {
            for (String url : set) {
                AliOSSUtil.deleteFile(url);
                System.out.println("删除图片：" + url);
            }
            //计算
            //删除redis中图片
            redisTemplate.delete(RedisConstants.UPLOAD_IMAGE);
            redisTemplate.delete(RedisConstants.USE_IMAGE);
        }
    }
}
